package edu.pw.treegrid.server.query;

import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

import edu.pw.treegrid.server.classmodel.BusinessModelData;
import edu.pw.treegrid.server.message.Path;
import edu.pw.treegrid.server.message.Request;
import edu.pw.treegrid.server.message.ResponseFetchChilds;
import edu.pw.treegrid.server.model.DomainField;
import edu.pw.treegrid.server.model.DomainObject;
import edu.pw.treegrid.server.reportmodel.Report;
import edu.pw.treegrid.server.service.DatabaseConnector;
import edu.pw.treegrid.server.service.TableRow2DomainObjectConverter;
import edu.pw.treegrid.server.service.TreeGridServiceImpl;


public class DatabasePrototype {
	
	private static DataSource DATA_SOURCE = null;
	
	public static DataSource getDataSource() {
		if(DATA_SOURCE == null) {
			BasicDataSource ds = new BasicDataSource();
			ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
			ds.setUsername("crm");
			ds.setPassword("crm");
			ds.setUrl("jdbc:oracle:thin:@127.0.0.1:1521/xe");
			DATA_SOURCE = ds;
		}
		return DATA_SOURCE;
	}
	
	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		TreeGridServiceImpl service = new TreeGridServiceImpl();
		service.setDataSource(getDataSource());
		service.setQueryGenerator(new QueryGenerator());
		service.setConverter(new TableRow2DomainObjectConverter());
		
		BusinessModelData data = new BusinessModelData();
		Request request = new Request();
		request.setFilter(null);
		request.setReport(getReportById(data, "7"));
		Path p = new Path();
		p.addNode(11);
		p.addNode(111);
		p.addNode(101);
		p.addNode("Klient 4");
		request.setHierarchy(data.R_Z_D_K_report7Hierarchy);
		request.setPath(p);
		
		
		ResponseFetchChilds response = new ResponseFetchChilds();
		service.fetchChilds(request, response);
		
		displayObjects(response.getDomainObjects());
	}
	
	private static Report getReportById(BusinessModelData data, String reportId) {
		if(reportId.equals("1")) {
			return data.report1;
		}
		else if(reportId.equals("2")) {
			return data.report2;
		}
		else if(reportId.equals("3")) {
			return data.report3;
		}
		else if(reportId.equals("4")) {
			return data.report4;
		}
		else if(reportId.equals("5")) {
			return data.report5;
		}
		else if(reportId.equals("6")) {
			return data.report6;
		}
		else if(reportId.equals("7")) {
			return data.report7;
		}
		else if(reportId.equals("1")) {
			return data.report1;
		}
		
		return null;
	}

	public static void displayObjects(List<DomainObject> objects) {
		final String sep = " | ";
		boolean first = true;
		for(DomainObject domainObject : objects) {
			if(first) {
				System.out.print("Name");
				System.out.print(sep);
				System.out.print("Path");
				System.out.print(sep);
				System.out.print("ParentPath");
				System.out.print(sep);
				for(DomainField df : domainObject.getDomainField()) {
					System.out.print(df.getReportColumn().getSimpleAttribute().getName());
					System.out.print(sep);
				}
				System.out.println();
				first = false;
			}
			System.out.print(domainObject.getName());System.out.print(sep);
			System.out.print(domainObject.getPath());System.out.print(sep);
			System.out.print(domainObject.getParentPath());System.out.print(sep);
			for(DomainField df : domainObject.getDomainField()) {
				System.out.print(df.getValue());
				System.out.print(sep);
			}
			System.out.println();
		}
	}

}
